package com.bitbakery.plugin.cl.lang.formatting;

import com.bitbakery.plugin.cl.lang.lexer.CLTokenTypes;
import com.intellij.formatting.Spacing;
import com.intellij.lang.ASTNode;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.tree.TokenSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class CLSpacingProcessor {
    private static final Spacing NO_SPACING = Spacing.createSpacing(0, 0, 0, false, 0);
    private static final Spacing SINGLE_SPACING = Spacing.createSpacing(1, 1000, 0, true, 1000);
    private static final Spacing NO_EOL_SPACING = Spacing.createSpacing(1, 1000, 0, false, 0);
    /**
     * Calculates spacing between two ambigiuous children
     *
     * @param parent     Parent block
     * @param leftChild  Left child node
     * @param rightChild Right child node
     * @param settings   Current code style settings
     * @return Spacing object
     */
    @Nullable
    public static Spacing getSpacing(@NotNull final CLBlock parent, @NotNull final ASTNode leftChild, @NotNull final ASTNode rightChild, CodeStyleSettings settings) {
        if (SpacingTokens.SPACING_AFTER.contains(leftChild.getElementType())) {
            return SINGLE_SPACING;
        }
        if (SpacingTokens.NO_SPACING_BEFORE.contains(rightChild.getElementType())) {
            return NO_SPACING;
        }
/*
        if (rightChild.getPsi() instanceof RCondition){
            return NO_EOL_SPACING;
        }
*/
        return null;
    }

    private interface SpacingTokens extends CLTokenTypes {
        /**
         * Always no spacing before these tokens
         */
        final TokenSet NO_SPACING_BEFORE = TokenSet.create();

        /**
         * Always at least single space after these tokens
         */
        final TokenSet SPACING_AFTER = TokenSet.create(SYMBOL);
    }
}
